#include "stdafx.h"
#include "XmlProcessing.h"

namespace xmlredirect
{
	MoveProcessing::MoveProcessing(const string& path):m_path(path)
	{}
	void MoveProcessing::Add(const xml::pnode& node)
	{
		HandleMoveTag(node);
	}

	void MoveProcessing::HandleMoveTag(const xml::pnode& node)
	{
		if ( hc::to_lower( node.get_name() ) == "move" )
		{
			MoveData data;
			xml::pnode atr = node >> xml::attribute("from");
			std::string tmp = atr.as_string();
			boost::replace_first(tmp, "\\\\", "\\\\?\\UNC\\");
			data.SetFrom(tmp);

			atr = node >> xml::attribute("to");
			tmp = atr.as_string();
			boost::replace_first(tmp, "\\\\", "\\\\?\\UNC\\");
			data.SetTo( tmp );

			xml::pnodelist children = node.get_children();
			for(xml::pnode child = children.nextnode(); child.valid(); child = children.nextnode() ) 
			{
				HandleWhatTag(child, data);
			}
			m_rules.push_back( boost::shared_ptr<IRule>(new Move<MoveData>(data)) );
		}
	}
	void MoveProcessing::HandleWhatTag(const xml::pnode& node, MoveData& data)
	{
		if ( hc::to_lower( node.get_name() )  == "what") 
		{
			xml::pnode atr = node >> xml::attribute("extension");
			WhatData what( atr.as_string() );
			xml::pnodelist children = node.get_children();
			for(xml::pnode child = children.nextnode(); child.valid(); child = children.nextnode() ) 
			{
				if ( hc::to_lower( child.get_name() ) == "storerkey")
				{
					xml::pnode name = child >> xml::attribute("name");
					what.AddObject( "storerkey", hc::to_lower( name.as_string() ) );
				} 
			}
			data.AddObject( what );
		}
	}

	list< boost::shared_ptr< IRule > > MoveProcessing::GetResult()
	{
		return m_rules;
	}

	string	MoveProcessing::GetPath()
	{
		return m_path;
	}

	//------------------------------------------------------------------
	SystemSettingProcessing::SystemSettingProcessing(const string& path):m_path(path)
	{}

	void SystemSettingProcessing::Add(const xml::pnode& node)
	{
		GetTagAttributeValue( node, "Threads", "number" );
		GetTagAttributeValue( node, "TimeOut", "sec" );
	
	}
	void SystemSettingProcessing::GetTagAttributeValue( const xml::pnode& node, const string& tagName, const string& tagAttributeName )
	{
		if ( node.get_name() == tagName )
		{
			xml::pnode atr = node >> xml::attribute( tagAttributeName );
			m_result.insert( pair<string, string>( node.get_name(), atr.as_string() ) );
		}
	}

	map<string, string> SystemSettingProcessing::GetResult()
	{
		return m_result;
	}

	string	SystemSettingProcessing::GetPath()
	{
		return m_path;
	}

	//------------------------------------------------------------------
	ConfigProcessing::ConfigProcessing(const string& xmlPath)
	{
		m_xml.load_xml(xmlPath);
	}
	void ConfigProcessing::Add( boost::shared_ptr<IProcessing>& sp_processing)
	{
		m_processing.push_back( sp_processing );
	}
	void ConfigProcessing::Run()
	{
		std::for_each(m_processing.begin(), m_processing.end(), boost::bind( &ConfigProcessing::Search , this, _1) );
	}
	
	void ConfigProcessing::Search( boost::shared_ptr<IProcessing>& proc )
	{
		xml::pnodelist nodeList = m_xml.get_root() >> xml::nodelist( proc->GetPath() );
		for(xml::pnode n = nodeList.nextnode(); n.valid(); n = nodeList.nextnode() ) 
		{ 
			proc->Add( n );
		}
	}
}